package com.bufpay.util;
import com.bufpay.pojo.PayModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.UUID;

public class PayUtil {

	private static Logger logger = LogManager.getLogger();

	public static String BASE_URL = "https://bufpay.com/api/pay/";
	public static String NOTIFY_URL = "http://7rx9fj.natappfree.cc/pay/notifyPay";
	public static String RETURN_URL = "http://7rx9fj.natappfree.cc/ok.html";
	public static String FEEDBACK_URL = "http://7rx9fj.natappfree.cc/no.html";

	public static String AID = "见后台";	//见后台
	public static String APP_SECRET = "见后台";	//见后台


	public static String getSign(PayModel payModel) {
		StringBuffer sb = new StringBuffer();
		sb.append(payModel.getName());
		sb.append(payModel.getPay_type());
		sb.append(payModel.getPrice());
		sb.append(payModel.getOrder_id());
		sb.append(payModel.getOrder_uid());
		sb.append(NOTIFY_URL);
		sb.append(RETURN_URL);
		sb.append(APP_SECRET);
		return MD5Util.encryption(sb.toString());
	}


	public static String getOrderIdByUUId() {
		int machineId = 1;// 最大支持1-9个集群机器部署
		int hashCodeV = UUID.randomUUID().toString().hashCode();
		if (hashCodeV < 0) {// 有可能是负数
			hashCodeV = -hashCodeV;
		}
		// 0 代表前面补充0;d 代表参数为正数型
		return machineId + String.format("%01d", hashCodeV);
	}

	public static boolean checkSign(String aoid, String order_id, String order_uid, String price, String pay_price, String sign) {
//		logger.info("支付回来的Key：" + sign);
		StringBuffer sb = new StringBuffer();
		sb.append(aoid);
		sb.append(order_id);
		sb.append(order_uid);
		sb.append(price);
		sb.append(pay_price);
		sb.append(APP_SECRET);
//		logger.info("验证的Key："+MD5Util.encryption(sb.toString()));
		return  sign.equals(MD5Util.encryption(sb.toString()));
	}
}
